﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using SurveySystem.Admin.Controllers;
using SurveySystem.Factory;
using SurveySystem.Model;
using SurveySystem.Repository;

namespace SurveySystem.Admin.Screens
{
    public partial class SurveyListForm : Form
    {
        public const string DATE_DISPLAY_FORMAT = "g";//"dd-MM-yyyy HH:mm";

        public ISurveyRepository SurveyRepository { get; set; }
        public ISurveyQuestionRepository QuestionRepository { get; set; }
        public ISurveyAnswerRepository AnswerRepository { get; set; }
        public ISurveyResponseRepository ResponseRepository { get; set; }
        public ISurveyResponseAnswerRepository ResponseAnswerRepository { get; set; }

        public SurveyListForm()
        {
            InitializeComponent();
        }

        private void SurveyListForm_Load(object sender, EventArgs e)
        {
            RefreshSurveyList();
        }

        public void RefreshSurveyList()
        {

            SurveyFactory factory = new SurveyFactory(SurveyRepository, QuestionRepository, AnswerRepository);

            //Console.WriteLine("Loading surveys");
            List<Survey> surveys = SurveyRepository.GetAllSurveys();

            ListView view = this.lvMainList;

            // clear what's there already
            view.Items.Clear();


            foreach (Survey s in surveys)
            {
                factory.PopulateSurvey(s);

                ListViewItem l = new ListViewItem();

                //Console.WriteLine("Loading responses for survey " + s.Title);
                List<Response> responses;
                try
                {
                    responses = ResponseRepository.GetResponsesForSurvey(s.SurveyID);
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex.Message);
                    responses = null;
                }

                l.Text = s.SurveyID.ToString();
                l.SubItems.Add(s.Title);
                l.SubItems.Add(s.Description);
                l.SubItems.Add(s.Questions.Count.ToString());
                l.SubItems.Add(s.OpenDate.ToString(DATE_DISPLAY_FORMAT));
                l.SubItems.Add(s.CloseDate.ToString(DATE_DISPLAY_FORMAT));
                l.SubItems.Add(s.Closed ? "Closed Manually" : !s.NoCloseDate && s.CloseDate < DateTime.Now ? "Expired" : "Open");
                if (responses != null)
                    l.SubItems.Add(responses.Count.ToString());
                else
                    l.SubItems.Add("err").ForeColor = Color.Red;

                l.Tag = s;

                view.Items.Add(l);

                //Console.WriteLine("Finished loading");
            }
        }

        private void btnAddSurvey_Click(object sender, EventArgs e)
        {
            SurveyFactory factory = new SurveyFactory(SurveyRepository, QuestionRepository, AnswerRepository);

            AddSurveyController controller = new AddSurveyController(factory);
            if (controller.ShowDialog() == DialogResult.OK)
                RefreshSurveyList();

        }

        private void btnEditSurvey_Click(object sender, EventArgs e)
        {
            // ensure there's a survey selected
            if (lvMainList.SelectedItems.Count == 0)
                return;

            Survey s = (Survey)(lvMainList.SelectedItems[0].Tag);

            SurveyFactory factory = new SurveyFactory(SurveyRepository, QuestionRepository, AnswerRepository);

            EditSurveyController controller = new EditSurveyController(factory, s);
            if (controller.ShowDialog() == DialogResult.OK)
                RefreshSurveyList();
        }

        private void btnCloseSurvey_Click(object sender, EventArgs e)
        {
            // ensure there's a survey selected
            if (lvMainList.SelectedItems.Count == 0)
                return;

            DialogResult res = MessageBox.Show("Are you sure you want to close this survey? This does not delete anything, and you can re-open later", "Confirm Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (res != System.Windows.Forms.DialogResult.OK)
                return;

            Survey s = (Survey)(lvMainList.SelectedItems[0].Tag);

            s.Closed = true;

            SurveyRepository.UpdateSurvey(s);

            RefreshSurveyList();
        }

        private void btnDeleteSurvey_Click(object sender, EventArgs e)
        {
            // ensure there's a survey selected
            if (lvMainList.SelectedItems.Count == 0)
                return;

            DialogResult res = MessageBox.Show("Are you sure you want to delete this survey and associated responses? If you want to access the results later, you should select \"No\" and then \"Close Survey\"", "Confirm Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (res != System.Windows.Forms.DialogResult.OK)
                return;

            Survey s = (Survey)(lvMainList.SelectedItems[0].Tag);

            SurveyFactory factory = new SurveyFactory(SurveyRepository, QuestionRepository, AnswerRepository);

            factory.DeleteSurvey(s);

            RefreshSurveyList();
        }

        private void btnResults_Click(object sender, EventArgs e)
        {
            // ensure there's a survey selected
            if (lvMainList.SelectedItems.Count == 0)
                return;

            Survey s = (Survey)(lvMainList.SelectedItems[0].Tag);

            CsvSurveyExportFactory csv = new CsvSurveyExportFactory(SurveyRepository, QuestionRepository, AnswerRepository, ResponseRepository, ResponseAnswerRepository);

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.FileName = s.Title + " results.csv";

            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                csv.ExportSurveyToCsvFile(s.SurveyID, sfd.FileName);
            }


        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshSurveyList();
        }

    }
}
